package com.example.administrator.zhongyichou.activity;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.view.PromptDialog1;
import com.example.administrator.zhongyichou.help.DateUtils;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.protocol.MemberInfoProtocol;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.view.PromptDialog;
import com.example.administrator.zhongyichou.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/23.
 * 描述：会员卡详情
 */

public class MemberDetailsActivity extends BaseActivity<MemberInfoProtocol> {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.btn_pay)
    Button btnPay;
    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.ll_balance_state)
    LinearLayout llBalanceState;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_id_card)
    TextView txtIdCard;
    @BindView(R.id.txt_status)
    TextView txtStatus;
    @BindView(R.id.txt_join_time)
    TextView txtJoinTime;
    @BindView(R.id.txt_effective_time)
    TextView txtEffectiveTime;
    @BindView(R.id.txt_num)
    TextView txtNum;
    @BindView(R.id.txt_balance)
    TextView txtBalance;
    @BindView(R.id.txt_bill)
    TextView txtBill;
    @BindView(R.id.txt_member_id)
    TextView txtMemberId;
    @BindView(R.id.txt_type1)
    TextView txtType1;
    @BindView(R.id.txt_type2)
    TextView txtType2;
    @BindView(R.id.txt_type3)
    TextView txtType3;
    private String memberID;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
    }

    @Override
    protected void responseData(MemberInfoProtocol mGsonBaseProtocal) {
        txtMemberId.setText("会员No." + mGsonBaseProtocal.getData().getMember_id());
        txtName.setText(mGsonBaseProtocal.getData().getMember_name());
        if (mGsonBaseProtocal.getData().getMember_num().length() == 18) {
            txtIdCard.setText(mGsonBaseProtocal.getData().getMember_num().substring(0, 4) + "**********" + mGsonBaseProtocal.getData().getMember_num().substring(mGsonBaseProtocal.getData().getMember_num().length() - 4, mGsonBaseProtocal.getData().getMember_num().length()));
        } else {
            txtIdCard.setText(mGsonBaseProtocal.getData().getMember_num());
        }
        switch (mGsonBaseProtocal.getData().getMember_status()) {
            case "0"://未激活
                txtStatus.setText("未激活");
                txtStatus.setTextColor(Color.parseColor("#999999"));
                break;
            case "1"://已缴费激活-进入考察期
                txtStatus.setText("观察期");
                txtStatus.setTextColor(Color.parseColor("#ffb40c"));
                break;
            case "2"://已过考察期-可以发起互助
                txtStatus.setText("已生效");
                txtStatus.setTextColor(Color.parseColor("#00a0e9"));
                break;
            case "3"://失效-余额不足，充值后直接进入
                txtStatus.setText("失效中");
                txtStatus.setTextColor(Color.parseColor("#f05d4d"));
                break;
        }
        if (mGsonBaseProtocal.getData().getPay_time().equals("0")) {
            txtJoinTime.setText("无");
        } else {
            txtJoinTime.setText(DateUtils.stampToDate(mGsonBaseProtocal.getData().getPay_time()));
        }
        if (mGsonBaseProtocal.getData().getActive_time().equals("0")) {
            txtEffectiveTime.setText("无");
        } else {
            txtEffectiveTime.setText(DateUtils.stampToDate(mGsonBaseProtocal.getData().getActive_time()));
        }
        txtNum.setText(mGsonBaseProtocal.getData().getMutual_num() + "次");
        txtBalance.setText(mGsonBaseProtocal.getData().getMember_cash() + "元");
        setType(mGsonBaseProtocal.getData().getMember_cash());
    }

    private void setType(double balance) {
        clear();
        if (balance == 0) {
            txtType3.setBackgroundColor(getResources().getColor(R.color.red1));
        } else if (balance < 50) {
            txtType2.setBackgroundColor(getResources().getColor(R.color.yellow));
        } else {
            txtType1.setBackgroundColor(getResources().getColor(R.color.appColor));
        }
    }

    private void clear() {
        txtType1.setBackgroundColor(getResources().getColor(R.color.black_text9));
        txtType2.setBackgroundColor(getResources().getColor(R.color.black_text9));
        txtType3.setBackgroundColor(getResources().getColor(R.color.black_text9));
    }

    @Override
    protected HttpLoader initHttpLoader() {
        memberID = getIntent().getStringExtra("member_id");
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(MemberInfoProtocol.class);
        Map map = new HashMap();
        map.put("user_id", MyApplication.getUserID());
        map.put("member_id", memberID);
        requestHttpLoader.setParams(NetConfig.MEMBER_INFO, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.member_details_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    private void initHead() {
        headerview.setTitle("会员卡");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @OnClick({R.id.ll_state, R.id.ll_balance_state, R.id.btn_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_state:
                PromptDialog promptDialog = new PromptDialog(this);
                promptDialog.show();
                break;
            case R.id.ll_balance_state:
                PromptDialog1 promptDialog1 = new PromptDialog1(this);
                promptDialog1.show();
                break;
            case R.id.btn_pay:
                break;
        }
    }
}
